package com.example.demo;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@RestController
public class uploadPic {
    @PostMapping("/uploadPic")//url: 'http://localhost:8080/postuser/uploadPic'
    public JSONObject uploadPic (@RequestBody JSONObject frontJson) throws IOException {
        System.out.println(frontJson);
        JSONObject json = new JSONObject();
        String type=frontJson.getString("type");
        String id=frontJson.getString("id");
        String base64=frontJson.getString("file");
        String filePath = null;
        //图片上传成功后，将图片的地址写到数据库D://images
        if(type.equals("post")){
            filePath="D:\\WeChat_Mini_Program\\BackEnd\\public\\post\\";
        }else if(type.equals("user")){
            filePath="D:\\WeChat_Mini_Program\\BackEnd\\public\\user\\";
            id=wxidgetuserid(id);
        }
        byte[] bs = new byte[1024];
        bs = Base64.getMimeDecoder().decode(base64);
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        file = new File(filePath+id+".png");
        try {
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        json.put("status","COMPLETE");
        return json;
    }
    public String wxidgetuserid(String wxid){
        Connection con;
        try {
            String driver="com.mysql.cj.jdbc.Driver";
            String url="jdbc:mysql://localhost:3306/jidazhidemai";

            // 数据库的用户名与密码，需要根据自己的设置
            String user="root";
            String password="gFL123456";
            Class.forName(driver);
            //注册JDBC驱动程 序
            //建立连接
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed()) {
                System.out.println("数据库连接成功");
            }

            //Statement stmt = con.createStatement();
//            String sql = "INSERT INTO tab1 (title,author,submission_date) VALUES ('li','xiao','2020-07028')";
            String sql = " select userid from user where wxid=?;";
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, wxid);
            ResultSet rs = preparedStatement.executeQuery();
            String userid="该用户未注册";
            if (rs.next()){
                userid = rs.getString("userid");
            }
            con.close();
            return userid;

        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动没有安装");

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
        return "false-出错了";
    }
}
